import {User} from '../user/user-model.js';
var user = new User();
Page({
  data: {
    modalOpened: false,
    modalContent: ""
  },
  onLoad(options) {
   this._loadData();
  },
  _loadData:function()
  {
    user.getMyreturn((data)=>{
      this.setData({
        returnArr:data.data
      })
    })
  },

  
  // 查看原因
  look(e) {
    let id = e.currentTarget.dataset.id;
    let data = this.data.returnArr;
    for(let i=0;i<data.length;i++)
    {
      if(id==data[i]['id'])
      {
        this.setData({
      modalOpened: true,
      modalContent:data[i].fail_reason,//失败原因的内容
    });
      }
    }
   
  },

  // 关闭弹框
  onModalClose() {
    this.setData({
      modalOpened: false,
    });
  },

  //买断
  pay(e)
  {
    var id = e.currentTarget.dataset.id;
    user.buyProductAfterOrder(id,(data)=>{
        var orderId = data.data.order_id;
        var orderNo = data.data.order_no;
        this.payOrder(orderId,orderNo);
    })
  },
  //下单
   payOrder:function(orderId,orderNo)
  {
    user.getReturnPreOrder(orderId,orderNo,(data)=>{
         var out_trade_no = data.data.alipay_trade_create_response.out_trade_no;
         var trade_no = data.data.alipay_trade_create_response.trade_no;
         my.tradePay({
           tradeNO:trade_no,
           success: (res) => {
             if(res.resultCode ==9000)
                {
                  user.returnNotify(orderId,(data)=>{
                    if(data.code ==1)
                    {
                     my.showToast({
                       type:'none',
                       content:"支付成功",
                       success: (res) => {
                         my.switchTab({
                           url: '/pages/user/user',
                         });
                       },
                     });
                    }
                  })

                }
           },
         });
    })
  },
});
